
import api from '../../http/apis/apis.js';
export default {
  namespaced: true,
  //下面写请求
  state: {

    serachData: {
      type: "username",
      value: ".",
    },
    pageData: {
      currentPage: 1,
      pageSize: 3,
      total: 0,
      totalpage: 0,
      data: [],
    },
    pageData1: [],


    Myid: {
      _id: "",

    }







  },
  mutations: {
    //个人信息传个id
    setMyadmin(state, newId) {


      state.Myid._id = newId._id

    },
    //翻页
    setPageData(state, newPagedata) {
      state.pageData = newPagedata


    },
    //传个人信息  
    setPageData1(state, newPagedata1) {
      state.pageData1 = newPagedata1



    },
    //姓名查询
    setSerachDataByusername(state, newSerachData) {
      state.serachData.type = "username"
      state.serachData.value = newSerachData

    },
    //类型查询
    setSerachDataBytype(state, newSerachType) {


      if (newSerachType == "permission") {

        state.serachData.value = "1"
        state.serachData.type = newSerachType
      } else if (newSerachType == "SuperPermission") {
        console.log("c")
        state.serachData.value = "9"
        state.serachData.type = "permission"
      } else {
        console.log("a")
        state.serachData.value = "."
        state.serachData.type = "username"
      }


    },
    //修改页面
    setPageSize(state, newPageszie) {

      state.pageData.pageSize = newPageszie
    },
    //当前页面
    setCurrent(state, newCurrentPage) {
      state.pageData.currentPage = newCurrentPage
    },


  },
  actions: {
    async addAdmin(context, obj) {

      const { data } = await api.admin.addAdmin(obj)


    },
    //获得所有管理员信息
    async getAllAdmins(context) {
      const { data } = await api.admin.getAllAdmins({
        serachData: context.state.serachData,
        currentPage: context.state.pageData.currentPage,
        pageSize: context.state.pageData.pageSize
      })

      if (data.status)
        context.commit('setPageData', data.data)

    },

    async login(context, adminData) {
      console.log(adminData);
      const { data } = await api.admin.login(adminData)

      localStorage.token = data.token
      return data
    },

    async deleteAdminByID(context, _id) {
      const { data } = await api.admin.deleteAdminByID(_id)


    },
    async getAdminById(context, _id) {
      const { data } = await api.admin.getAdminById(_id);

      if (data.status)
        context.commit('setPageData1', data.data)

      return data.data

    },


    async updateAdmin(context, data1) {
      const { data } = await api.admin.updateAdmin(data1);

    },
    async isLogin(context) {
      const { data } = await api.admin.isLogin()
      return data.data
    }
  }

}